//
//  PainterIndexHJJT.m
//  YGMobile
//
//  Created by zhanglu on 2017/1/29.
//  Copyright © 2017年 Elephants Financial Service. All rights reserved.
//

#import "PainterIndexHJJT.h"
#import "HJJT.h"

static CGFloat const kCrossLineWidth = 62.5;
static NSString * const kHJJTTextColor = @"#2b2f33";
static NSString * const kHJJTTextName = @"VERDANA";
//static NSString * const kHJJTTextFont = @"13";

using namespace std;
using namespace ytx;
@implementation PainterIndexHJJT

- (void)drawRect:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition
{
    KLineData * data = (KLineData *)indexPosition->getData();
    
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();

    CGFloat hollowY = 0.0, zoomX;
    CGFloat tempX = rect.getBeginX();
    CGFloat KX = tempX;
    CGFloat lineWidth = style.quoteLineWidth;
    LinearGradientDirection direction = LinearGradientFromBottomToUp;
    int startIndex = indexPosition->getStartDataIndex(), endIndex = indexPosition->getEndDataIndex();
    
    zoomX = rect.getZoomX();
    tempX += zoomX / 2.0;
    
    [ctx setLineWidth: lineWidth];
    
    vector<std::vector<double> > & indexData = data->indexData;
    vector<std::vector<double> > & lineData = data->lineData;
    if (indexData.begin() == indexData.end()) {
        return;
    }
    std::vector<double> & positions = indexData[0];
    std::vector<double> & beginPrices = lineData[0];
    std::vector<double> & endPrices = lineData[1];
    std::vector<double> & upLines = indexData[1];
    std::vector<double> & bottoms = indexData[2];
    std::vector<double> & sUpLines = indexData[3];
    std::vector<double> & upMids = indexData[4];
    std::vector<double> & downMids = indexData[5];
    std::vector<double> & sBottoms = indexData[6];

    NSString *darkColor, *lightColor;
    for (std::vector<double>::iterator it = positions.begin() + startIndex; it != MIN(positions.begin() + endIndex,positions.end()); ++it) {
        if (it >= positions.end()) {
            break;
        }
        size_t index = it-positions.begin();
        
        HJJTPosition::HJJTPosition position = (HJJTPosition::HJJTPosition)positions[startIndex];
        
        position = (HJJTPosition::HJJTPosition)(*it);
        if (position == HJJTPosition::Empty) {
            tempX += zoomX;
            KX += zoomX;
            continue;
        }
        else if (position == HJJTPosition::Bear) { // 空头区域
            darkColor = [NSString stringWithUTF8String:(style.HJJTTypeDownBackground).c_str()];
            lightColor = [NSString stringWithUTF8String:(style.HJJTTypeDownLightBackground).c_str()];
            hollowY = rect.getPxY(beginPrices[index]);
            direction = LinearGradientFromBottomToUp;
        }
        else if (position == HJJTPosition::Bull) {
            darkColor = [NSString stringWithUTF8String:(style.HJJTTypeUpBackground).c_str()];
            lightColor = [NSString stringWithUTF8String:(style.HJJTTypeUpLightBackground).c_str()];
            hollowY = rect.getPxY(endPrices[index]);
            direction = LinearGradientFromUpToBottom;
        }
        
        CGFloat height = fabs(rect.getPxY(beginPrices[index]) - rect.getPxY(endPrices[index]));//?:1;
        
        [ctx drawLinearGradientInRect:CGRectMake(KX, hollowY - height, zoomX, height) direction:direction startColor:lightColor endColor:darkColor];
        
        tempX += zoomX;
        KX += zoomX;
    }
    
    BOOL isHigerHJJTGranted = indexPosition->hjjtGJPermission();
    if (isHigerHJJTGranted) {//高级版
        const CGFloat x1 = rect.getBeginX();
        const CGFloat x2 = rect.getEndX();
        const int textLineCout = ceil(kCrossLineWidth/zoomX);
        const int showLineCount = MIN(20, indexPosition->getShowViewCount() - textLineCout);
        
        const int lastData = (int)positions.size() -1;//MIN(indexPosition->totalDataCount, (int)positions.size()) -1;
//        NSLog(@"indexPosition->totalDataCount:%d>=positions.size():%d  lastData:%d  startIndex:%d,  endIndex:%d  ShowViewCount:%d  showLineCount:%d  textLineCout:%d", indexPosition->totalDataCount, (int)positions.size(), lastData,    startIndex,endIndex,indexPosition->getShowViewCount(),showLineCount,textLineCout);
        if (endIndex >= lastData - showLineCount-textLineCout) {
            size_t index = lastData;
            
            const CGFloat distanceOfMovingHigerLine = /*(int)positions.size()*/indexPosition->totalDataCount - endIndex;//>0时右移 高级版线
            const CGFloat startX = x1 + rect.getRectWidth() - showLineCount*zoomX - textLineCout*zoomX  + (distanceOfMovingHigerLine>0?distanceOfMovingHigerLine:0)*zoomX;
            const CGFloat crossLineX = startX + showLineCount*zoomX;
            
            const CGFloat upLinesY = rect.getPxY(upLines[index]);
            const CGFloat sUpLinesY = rect.getPxY(sUpLines[index]);
            const CGFloat upMidsY = rect.getPxY(upMids[index]);
            const CGFloat downMidsY = rect.getPxY(downMids[index]);
            const CGFloat sBottomsY = rect.getPxY(sBottoms[index]);
            const CGFloat bottomsY = rect.getPxY(bottoms[index]);
            const CGFloat crossLineWidth = textLineCout*zoomX;;
            const CGFloat linearGradientY = (upMidsY+downMidsY)/2.0;
            
            //    渐进色
            [ctx drawLinearGradientInRect:CGRectMake(crossLineX, upLinesY, crossLineWidth, linearGradientY-upLinesY) direction:LinearGradientFromLeftToRight startColor:[NSString stringWithUTF8String:((string)"007ac1a9").c_str()] endColor:[NSString stringWithUTF8String:((string)"7ac1a9").c_str()]];
            [ctx drawLinearGradientInRect:CGRectMake(crossLineX, linearGradientY, crossLineWidth, bottomsY-linearGradientY) direction:LinearGradientFromLeftToRight startColor:[NSString stringWithUTF8String:((string)"00ea7e7e").c_str()] endColor:[NSString stringWithUTF8String:((string)"ea7e7e").c_str()]];
            
            //    画线
            [ctx setStrokeStyle:@"#ffbb34"];
            [ctx setLineWidth:style.crosshairsWidth];
            [ctx drawLineWithX1:startX withY1:upLinesY withX2:x2 withY2:upLinesY];//黄金分割线的顶部价格线
            [ctx drawLineWithX1:startX withY1:sUpLinesY withX2:x2 withY2:sUpLinesY];//次顶部价格
            [ctx drawLineWithX1:startX withY1:upMidsY withX2:x2 withY2:upMidsY];//上中部价格
            [ctx drawLineWithX1:startX withY1:downMidsY withX2:x2 withY2:downMidsY];//下中部价格
            [ctx drawLineWithX1:startX withY1:sBottomsY withX2:x2 withY2:sBottomsY];//次底部价格
            [ctx drawLineWithX1:startX withY1:bottomsY withX2:x2 withY2:bottomsY];//底部价格
            [ctx drawLineWithX1:crossLineX withY1:upLinesY withX2:crossLineX withY2:rect.getPxY(bottoms[index])];//黄金分割线的垂直线
            
            //    文字
            NSString * const kHJJTTextFont = [NSString stringWithFormat:@"%d", MIN(int(MIN(fabs(sUpLinesY-upLinesY), fabs(bottomsY-sBottomsY))-2), 14)];
            [ctx fillHJJTTexts:@"高抛区" withCGRect:CGRectMake(crossLineX, upLinesY, crossLineWidth, sUpLinesY-upLinesY) withColor:kHJJTTextColor withFontName:kHJJTTextName withSize:kHJJTTextFont withAnchor:CenterTop];
            [ctx fillHJJTTexts:@"关注区" withCGRect:CGRectMake(crossLineX, sUpLinesY, crossLineWidth, upMidsY-sUpLinesY) withColor:kHJJTTextColor withFontName:kHJJTTextName withSize:kHJJTTextFont withAnchor:CenterTop];
            [ctx fillHJJTTexts:@"观察区" withCGRect:CGRectMake(crossLineX, upMidsY, crossLineWidth, downMidsY-upMidsY) withColor:kHJJTTextColor withFontName:kHJJTTextName withSize:kHJJTTextFont withAnchor:CenterMiddle];
            [ctx fillHJJTTexts:@"关注区" withCGRect:CGRectMake(crossLineX, downMidsY, crossLineWidth, sBottomsY-downMidsY) withColor:kHJJTTextColor withFontName:kHJJTTextName withSize:kHJJTTextFont withAnchor:CenterBottom];
            [ctx fillHJJTTexts:@"低吸区" withCGRect:CGRectMake(crossLineX, sBottomsY, crossLineWidth, bottomsY-sBottomsY) withColor:kHJJTTextColor withFontName:kHJJTTextName withSize:kHJJTTextFont withAnchor:CenterBottom];
        }
    }
}

@end
